/*************************************************************************
> File Name: printer.cpp
> Author: hzy
> Created Time: 2023-08-12 09:27:45
> Description:
************************************************************************/
#include <iostream>

using namespace std;

class Printer
{
private:
    // 静态成员变量，用于保存唯一的实例对象
    static Printer *printer;

    // 将构造函数、析构函数和拷贝构造函数设置为私有，防止外部直接创建对象和拷贝对象
    Printer()
    {
        cout << "Printer()" << endl;
    }
    ~Printer()
    {
        cout << "~Printer()" << endl;
    }
    Printer(const Printer &)
    {
        cout << "Printer(const Printer &)" << endl;
    }

public:
    // 如果实例对象为空，则创建一个新的实例
    static Printer *getPrinter()
    {
        if (printer == NULL)
        {
            cout << "123" << endl;
            printer = new Printer();
        }
        return printer;
    }

    void PrinterText(string name, string text)
    {
        cout << name << ":" << text << endl;
    }
};

// 静态成员变量需要在类外进行初始化
Printer *Printer::printer = nullptr;

class Person
{
private:
    string name;
    string text;
    Printer *p;

public:
    Person(string myname, string mytext)
    {
        name = myname;
        text = mytext;
        p = Printer::getPrinter();
    }

    void show()
    {
        p->PrinterText(name, text);
    }
};

int main()
{
    Person person1("hzy", "888");
    Person person2("aaa", "222");

    person1.show();
    person2.show();

    return 0;
}